package com.znz.service;

import com.znz.domain.Tcustomer;
import com.znz.domain.Torder;
import com.znz.domain.Treturnvisit;
import com.znz.util.common.Page;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: duyuxin
 * Date: 15-4-27
 * Time: 下午10:09
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ReturnVisitService {

    /**
     * @param userName  用户名称
     * @param state     状态
     * @param page
     * @return
     */
       public Page<Treturnvisit> getReturnVisitByWhere(String userName,int state,Page page){
            try{
                StringBuilder where=new StringBuilder();
                List<Object> params=new ArrayList<Object>();
                if(null!=userName){
                    where.append(" o.userName like '%?%' ");
                    params.add(userName);
                }
              page.setTotal((int)Treturnvisit.countTreturnvisits());
              List<Treturnvisit> treturnvisitList=Treturnvisit.getReturnVisitByWhere(where.toString(),params,page.getOffset(),page.getOffset()+page.getLimit());
              page.setRows(treturnvisitList);
              return page;
            }catch (Exception e){
                e.printStackTrace();
            }
           return page;
       }
    /**
     * 方法说明:保存回访信息
     */
    public Treturnvisit saveTcustomer(JSONObject tcustomer){
        Treturnvisit treturnvisit=this.getTreturnVisit(tcustomer);
        if(null!=treturnvisit){
            return treturnvisit;
        }
         return null;
    };
    private Treturnvisit getTreturnVisit(JSONObject returnVisit){
        Treturnvisit treturnvisit=new Treturnvisit();
        Long orderId=returnVisit.getLong("order_id");
        Torder torder=Torder.findTorder(orderId);
        if(null!=torder){
            treturnvisit.setTorder(torder);
            treturnvisit.setOrderNo(torder.getOrederNo());
        }
        try {
            treturnvisit.setCreateDate(new Date());
            SimpleDateFormat SimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH");
            treturnvisit.setReturnVisitDate(SimpleDateFormat.parse(returnVisit.getString("ReturnVisitDate")));
        } catch (ParseException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        treturnvisit.setType(returnVisit.getString("type"));
        treturnvisit.setCUserId(Tcustomer.findTcustomer(returnVisit.getLong("cUserId")));
        treturnvisit.setRemark(returnVisit.getString("remark"));
        treturnvisit.setIsReturnVisit(returnVisit.getString("isReturnVisit"));
        treturnvisit.setReturnVisitRecord(returnVisit.getString("returnVisitRecord"));
        treturnvisit.setCreateDate(new Date());
        treturnvisit.setUpdateDate(null);
        treturnvisit.persist();
        return  treturnvisit;
    };
}
